<?php
header("Content-Type: text/html;charset=utf-8");
// php最大执行时间设置为：半个小时，php原来默认为30秒，爬不完
ini_set('max_execution_time', '0');
include '../vendor/autoload.php';// 使用composer安装后引入目录
include_once 'class/Mysql.php';
use QL\QueryList;

try {
//    $mysql = Mysql::getMysql();
//    $obj = $mysql->query('select id,url  from source where status = 0  LIMIT 500');
//    $books = $obj->fetchAll();
//    foreach ($books as $v){
//        $result =pickBook($v['url']);
//        if($result){
//           $mysql->query('update source set status = 1  where id= '.$v['id']);
//        }
//    }
    $data = file_get_contents('data/empty.csv');
    $data = explode("\n",$data);
    foreach ($data as $book_url){
        pickBook($book_url);
    }



}catch (Exception $e) {
    error_log(json_encode($data, 258) . PHP_EOL, 3, 'logs/exception_data.log');
    $exceptionlog = '文件 ['.$e->getFile().'] 第 '.$e->getLine().' 行出现未处理的异常'.PHP_EOL;
    $exceptionlog .= '代码:'.$e->getCode().PHP_EOL;
    $exceptionlog .= '信息:'.$e->getMessage().PHP_EOL;
    $exceptionlog .= '跟踪信息：'.$e->getTraceAsString();
    //记录错误
    error_log(date('Y-m-d H:i:s') . print_r($exceptionlog, 1) . PHP_EOL, 3, 'logs/'.date('Y-m-d').'_'.'exception.log');
}

function pickBook($book_url){
    $mysql = Mysql::getMysql();
    $html = iconv('GBK', 'UTF-8', file_get_contents($book_url));
    $rules = array(
        'book_name' => array('#xinxi .xx li:eq(0)', 'text'),    //获取纯文本格式的标题,并调用回调函数1
        'cover' => array('#xinxi img', 'src'),
        'cate' => array('#xinxi li:eq(1)', 'text'),
        'author_name' => array('#xinxi li:eq(2) a', 'text'),
        'last_time' => array('#xinxi li:eq(3)', 'text'),
        'last_page' => array('#xinxi li:eq(4) a', 'text'),
        'describe' => array('#xinxi .bk', 'text'),
    );
    $data = QueryList::html($html)->rules($rules)->queryData();
    if (empty($data['book_name'])) {
        error_log($book_url . PHP_EOL, 3, 'data/empty.csv');
       return false;
    }
    //处理分类
    $data['cate'] = trim(str_ireplace('分类：', '', $data['cate']));
    $obj = $mysql->query('select cate_id from book_cate where cate_name ="' . trim($data['cate']) . '" LIMIT 1');
    $book_cate = $obj->fetch();
    if (empty($book_cate)) {
        $sql = 'insert into book_cate (`cate_name`) values ("' . trim($data['cate']) . '")';
        $obj = $mysql->query($sql);
        if (!$obj) {
            error_log($sql . PHP_EOL, 3, 'logs/cate.log');
        }
        $obj = $mysql->query('select cate_id from book_cate where cate_name ="' . $data['cate'] . '" LIMIT 1');
        $book_cate = $obj->fetch();
    }

    $cate_id = $book_cate['cate_id'];

    //处理作者id
    $data["author_name"] = trim($data["author_name"]);
    $obj = $mysql->query('select author_id from author where author_name ="' . $data['author_name'] . '" LIMIT 1');
    $author = $obj->fetch();
    if (empty($author)) {
        $sql = 'insert into author (`author_name`) values ("' . $data['author_name'] . '")';
        $obj = $mysql->query($sql);
        if (!$obj) {
            error_log($sql . PHP_EOL, 3, 'logs/author.log');
        }
        $obj = $mysql->query('select author_id from author where author_name ="' . $data['author_name'] . '" LIMIT 1');
        $author = $obj->fetch();
    }
    $author_id = $author['author_id'];

    //组装书籍数据
    $book_name = $data['book_name'];


    $cover = 'https:' . $data['cover'];
    $book_name = $data['book_name'];
    $last_time = date('Y-m-d H:i:s', strtotime(trim(str_ireplace('更新时间：', '', $data["last_time"]))));
    $last_page = str_ireplace('第', '', $data['last_page']);
    $last_page_arr = explode("章", $last_page);
    $total_pages = cn2num($last_page_arr[0]);
    $last_page = '第' . $total_pages . "章 " . $last_page_arr[1];
    $describe = $data['describe'];

    $obj = $mysql->query('select book_id from books where book_name ="' .$book_name. '" LIMIT 1');
    $book_is_exits = $obj->fetch();
    if($book_is_exits){
        error_log(json_encode($book_url,[$book_name,$cate_id,$cover,$author_id,$describe,$last_time,$last_page,$total_pages],258) . PHP_EOL, 3, 'data/exists.csv');
        return false;
    }

    try {
        $sql = 'INSERT INTO books (`book_name`,`cate_id`,`cover`,`author_id`,`describe`,`last_time`,`last_page`,`total_pages`,`update_time`,`create_time`) values ( "' . $book_name . '", "' . $cate_id . '", "' . $cover . '", "' . $author_id . '", "' . $describe . '", "' . $last_time . '", "' . $last_page . '", "' . $total_pages . '", now(),now()) ';
        $obj = $mysql->query($sql);
        //获取书籍id

        $book_obj = $mysql->query('select book_id from books where book_name ="' .$book_name. '" LIMIT 1');
        $book_res = $book_obj->fetch();

        //写入数据源表
        $mysql->query('insert into book_source (`book_id`,`source_url`) VALUES ("'.$book_res['book_id'].'","'.$book_url.'")');

        return $obj = $mysql->query($sql);


    } catch (Exception $e) {
        error_log($sql . PHP_EOL, 3, 'logs/book_sql_exception_data.log');
        $exceptionlog = '文件 [' . $e->getFile() . '] 第 ' . $e->getLine() . ' 行出现未处理的异常' . PHP_EOL;
        $exceptionlog .= '代码:' . $e->getCode() . PHP_EOL;
        $exceptionlog .= '信息:' . $e->getMessage() . PHP_EOL;
        $exceptionlog .= '跟踪信息：' . $e->getTraceAsString();
        //记录错误
        error_log(date('Y-m-d H:i:s') . print_r($exceptionlog, 1) . PHP_EOL, 3, 'logs/' . date('Y-m-d') . '_' . 'book_sql_exception.log');
    }
}


function cn2num($string)
{
    if(is_numeric($string)){
        return $string;
    }
    // '仟' => '千','佰' => '百','拾' => '十',
    $string = str_replace('仟', '千', $string);
    $string = str_replace('佰', '百', $string);
    $string = str_replace('拾', '十', $string);
    $num = 0;
    $wan = explode('万', $string);
    if (count($wan) > 1) {
        $num += cn2num($wan[0]) * 10000;
        $string = $wan[1];
    }
    $qian = explode('千', $string);
    if (count($qian) > 1) {
        $num += cn2num($qian[0]) * 1000;
        $string = $qian[1];
    }
    $bai = explode('百', $string);
    if (count($bai) > 1) {
        $num += cn2num($bai[0]) * 100;
        $string = $bai[1];
    }
    $shi = explode('十', $string);
    if (count($shi) > 1) {
        $num += cn2num($shi[0] ? $shi[0] : '一') * 10;
        $string = $shi[1] ? $shi[1] : '零';
    }
    $ling = explode('零', $string);
    if (count($ling) > 1) {
        $string = $ling[1];
    }
    $d = array(
        '一' => '1','二' => '2','三' => '3','四' => '4','五' => '5','六' => '6','七' => '7','八' => '8','九' => '9',
        '壹' => '1','贰' => '2','叁' => '3','肆' => '4','伍' => '5','陆' => '6','柒' => '7','捌' => '8','玖' => '9',
        '零' => 0, '0' => 0, 'O' => 0, 'o' => 0,
        '两' => 2
    );
    return $num + @$d[$string];
}




?>